package com.useeinfo.oa.open.api.maintainOrder;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.oa.common.utils.NumberUtil;
import com.useeinfo.oa.modules.maintaining.biz.MaintainOrderBiz;
import com.useeinfo.oa.modules.maintaining.entity.MaintainOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @auther: bin.liu
 * @Date: 2019/1/24 15:40
 * @Description: 维保工单图表接口
 */
@RestController
@RequestMapping("/maintainCharts")
public class MaintainChartsAPI {

    @Autowired
    private MaintainOrderBiz maintainOrderBiz;

    @Autowired
    private HttpServletRequest httpServletRequest;

    /**
     * 管理看板图表数据
     * @param type week 周,  month 月, quarter 季度
     * @param enterpriseId 企业id
     * @return JSONObject
     */
    @RequestMapping("/getOrderData")
    public JSONObject getOrderData(String type, String enterpriseId) {
        JSONObject result = new JSONObject();
        JSONArray dateTimes = new JSONArray();
        JSONArray jsonArray = new JSONArray();
        JSONArray totalArray = new JSONArray();
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0); // 获取当月
        c.set(Calendar.DAY_OF_MONTH, 1); // 获取当月的第一天

        Date startDateTime = new Date();
        Date endDateTime = new Date();
        Date now = new Date();
        List<String> newDates = new ArrayList<>();

        JSONArray allOrders = new JSONArray();
        JSONArray completedOrders = new JSONArray();
        if ("week".equals(type)) {
            startDateTime = DateTimeUtils.getFirstDateOfWeek(now);
            endDateTime = now;
            newDates = DateTimeUtils.getDates(startDateTime, endDateTime, "M/d");
        }
        if ("month".equals(type)) {
            startDateTime = c.getTime(); // 获取了当月第一天的时间
            endDateTime = now; // 当前时间
            newDates = DateTimeUtils.getDates(startDateTime, endDateTime, "MM-dd");
        }
        if ("quarter".equals(type)) {
            int season = DateTimeUtils.getSeason(now);
            for (int i = 1; i <= season; i++) {
                newDates.add(NumberUtil.convert(i) + "季度");
                allOrders = maintainOrderBiz.getbetween(enterpriseId, DateTimeUtils.getStartDateBySeason(i), DateTimeUtils.getEndDateBySeason(i), 2, false);
                completedOrders = maintainOrderBiz.getbetween(enterpriseId, DateTimeUtils.getStartDateBySeason(i), DateTimeUtils.getEndDateBySeason(i), 2, true);
                int allTotal = 0;
                int allComplete = 0;
                if (allOrders.size() > 0) {
                    for (int a = 0; a < allOrders.size(); a++) {
                        JSONArray completeObject = completedOrders.getJSONArray(a);
                        JSONArray allObject = allOrders.getJSONArray(a);
                        allTotal += allObject.getIntValue(1);
                        allComplete += completeObject.getIntValue(1);
                    }
                }
                if (allTotal == 0) {
                    jsonArray.add(0);
                } else {
                    jsonArray.add((allComplete * 100 / allTotal * 100) / 100);
                }
                totalArray.add(allTotal);
                dateTimes.addAll(newDates);
            }
        }
        if (!"quarter".equals(type)) {
            dateTimes.addAll(newDates);
            List<String> dates = DateTimeUtils.getDates(startDateTime, endDateTime, "yyyy-MM-dd");
            allOrders = maintainOrderBiz.getbetween(enterpriseId, dates.get(0), dates.get(dates.size() - 1), 2, false);
            completedOrders = maintainOrderBiz.getbetween(enterpriseId, dates.get(0), dates.get(dates.size() - 1), 2, true);


            if (completedOrders.size() > 0) {
                for (int i = 0; i < allOrders.size(); i++) {
                    JSONArray completeObject = completedOrders.getJSONArray(i);
                    JSONArray allObject = allOrders.getJSONArray(i);
                    int total = allObject.getIntValue(1);
                    int complete = completeObject.getIntValue(1);
                    if (total == 0) {
                        jsonArray.add(0);
                    } else {
                        jsonArray.add((complete * 100 / total * 100) / 100);
                    }
                    totalArray.add(total);
                }
            }
        }

        JSONObject ppmDataObject1 = new JSONObject();
        JSONObject ppmDataObject2 = new JSONObject();

        ppmDataObject1.put("barWidth", 10);
        ppmDataObject1.put("data", totalArray);
        ppmDataObject1.put("name", "PPM");
        ppmDataObject1.put("type", "bar");

        ppmDataObject2.put("yAxisIndex", 1);
        ppmDataObject2.put("data", jsonArray);
        ppmDataObject2.put("name", "completion rate（%）");
        ppmDataObject2.put("type", "line");

        result.put("0", ppmDataObject1);
        result.put("1", ppmDataObject2);
        result.put("2", dateTimes);
        return result;
    }

    /**、
     * 班组人员接单数
     * @param organizationId
     * @param start
     * @param end
     * @return
     */
    @RequestMapping("/getOrganizationReceiveNum")
    public JSONArray getOrganizationReceiveNum(String organizationId,String start, String end) {
        List<Object> objects=maintainOrderBiz.getOrganizationReceiveNum(organizationId, start, end);
        JSONArray nowData = new JSONArray();
        objects.forEach(y -> {
            nowData.add(net.sf.json.JSONArray.fromObject(y).getInt(0));
        });
        return nowData;

    }


    /**
     *  部门维保人员工时统计
     */
    @RequestMapping("/getHoursData")
    public JSONArray getHoursChartsData(String startDateTime, String endDateTime,String organizationId,String enterpriseId){
        return maintainOrderBiz.getHoursChartsData(startDateTime,endDateTime,organizationId,enterpriseId);
    }

    /**
     * PC端维护保养工单数量统计
     */
    @RequestMapping("/maintainReportNum")
    public JSONObject maintainReportNum(String enterpriseId){
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("maintainToday",maintainOrderBiz.maintainReportNum(enterpriseId,"1"));
        jsonObject.put("maintainHour",maintainOrderBiz.maintainReportNum(enterpriseId,"2"));
        jsonObject.put("maintainMonth",maintainOrderBiz.maintainReportNum(enterpriseId,"3"));
        jsonObject.put("maintainComplete",maintainOrderBiz.maintainReportNum(enterpriseId,"4"));
        return jsonObject;
    }
}
